﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace HGZP.PLL
{
    /// <summary> 
    /// 功能：Cookie操作类 
    /// 作者：许金杰
    /// 时间：2011.6.25
    /// </summary> 
    public class Cookie
    {
        /// <summary> 
        /// 保存一个Cookie 
        /// </summary> 
        /// <param name="CookieName">Cookie名称</param> 
        /// <param name="CookieValue">Cookie值</param> 
        /// <param name="CookieTime">Cookie过期时间,0为关闭页面失效</param> 
        public static void SaveCookie(string CookieName, string CookieValue, DateTime CookieTime)
        {
            HttpCookie myCookie = new HttpCookie(CookieName);
            myCookie.Value = CookieValue;
            if (CookieTime.ToString() != DateTime.Now.AddDays(0).ToString())//判断是否是0
            {
                myCookie.Expires = CookieTime;
            }
            if (HttpContext.Current.Response.Cookies[CookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(CookieName);
            }
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }
        /// <summary>
        /// 保存带有子键的Cookie
        /// </summary>
        /// <param name="CookieName">cookie名字</param>
        /// <param name="CookieHashtable">哈希表键值对</param>
        /// <param name="CookieTime">Cookie过期时间,0为关闭页面失效</param>
        public static void SaveCookie(string CookieName, Hashtable CookieHashtable, DateTime CookieTime)
        {
            HttpCookie myCookie = new HttpCookie(CookieName);
            foreach (DictionaryEntry de in CookieHashtable)
            {
                myCookie.Values[de.Key.ToString().Trim()] = de.Value.ToString().Trim();
            }
            if (CookieTime.ToString() != DateTime.Now.AddDays(0).ToString())//判断是否是0
            {
                myCookie.Expires = CookieTime;
            }
            if (HttpContext.Current.Response.Cookies[CookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(CookieName);
            }
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }
        /// <summary> 
        /// 取得CookieValue 
        /// </summary> 
        /// <param name="CookieName">Cookie名称</param> 
        /// <returns>Cookie的值</returns> 
        public static string GetCookie(string CookieName)
        {
            HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName];
            if (myCookie != null)
            {
                return myCookie.Value;
            }
            else
            {
                return null;
            }
        }
        /// <summary> 
        /// 清除CookieValue 
        /// </summary> 
        /// <param name="CookieName">Cookie名称</param> 
        public static void ClearCookie(string CookieName)
        {
            HttpCookie myCookie = new HttpCookie(CookieName);
            DateTime now = DateTime.Now;
            myCookie.Expires = now.AddYears(-2);
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }
        /// <summary>
        /// 判断用户是否已经登录
        /// </summary>
        /// <returns></returns>
        public static bool JudgmentCookie()
        {
            if (Cookie.GetCookie("sGuid") == null || Cookie.GetCookie("sUserID") == null)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <returns></returns>
        public static string GetsUserName()
        {
            if (!JudgmentCookie())
            {
                return null;
            }
            return Cookie.GetCookie("sUserID");
        }
        /// <summary>
        /// 获取主键
        /// </summary>
        /// <returns></returns>
        public static string GetsGuid()
        {
            if (!JudgmentCookie())
            {
                return null;
            }
            return Cookie.GetCookie("sGuid");
        }
        /// <summary>
        /// 注销登录
        /// </summary>
        public static void CancellationLogin()
        {
            if (JudgmentCookie())
            {
                ClearCookie("sGuid");
                ClearCookie("sUserID");
            }
        }
    }
}